package com.mariusz.data.reports;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;


import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.DocumentName;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaSizeName;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;

import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;

import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mariusz.data.dto.Payment;
import com.mariusz.data.dto.Rentier;
import com.mariusz.utils.Formats;


import java.net.URL;


public class PaymentReport {
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	ByteArrayOutputStream stream;
	ByteArrayInputStream is;
	BaseColor blackColor = BaseColor.BLACK;
	private Rentier rentier;

	public PaymentReport(Rentier rentier) {
		this.rentier = rentier;
	}

	public void preparePdf() {
		try {
			stream = new ByteArrayOutputStream();
			PdfWriter writer = PdfWriter.getInstance(document, stream);

			document.open();
			try{
			URL url = ClassLoader.getSystemResource("com/mariusz/resources/logo_mdream.png");
	        Image image = Image.getInstance(url);
	        image.scaleAbsolute(160f, 70f);
	        document.add(image);
	        
			}catch(Exception e){
				e.printStackTrace();
			}
			
			Paragraph par = new Paragraph("Name: " + rentier.getName(),
					new Font(FontFamily.HELVETICA, 11, Font.BOLDITALIC, blackColor) );
			
			document.add(par);
			par = new Paragraph("Date: " + Formats.formatCossyDate(new Date()),
					new Font(FontFamily.HELVETICA, 11, Font.BOLDITALIC, blackColor) );
			
			document.add(par);
			if(rentier.getProperty() != null){
				par = new Paragraph("Address: " + rentier.getProperty().getName()+" "+rentier.getProperty().getAddress(),
						new Font(FontFamily.HELVETICA, 11, Font.BOLDITALIC, blackColor) );
			}else{
				par = new Paragraph("Address: ",
						new Font(FontFamily.HELVETICA, 11, Font.BOLDITALIC, blackColor) );
			}
		
			
			document.add(par);
			document.add(new Paragraph(" "));
			document.add(new Paragraph(" "));
			par = new Paragraph("Payment Records:", 
					new Font(FontFamily.HELVETICA, 11, Font.BOLDITALIC, blackColor) );
			
			document.add(par);

			document.add(new Paragraph(" "));

			PdfPTable table = new PdfPTable(3);

			PdfPCell c1 = new PdfPCell(new Phrase("Payment no."));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Due date"));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Amount"));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

//			c1 = new PdfPCell(new Phrase("Payed"));
//			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
//			table.addCell(c1);

			table.setHeaderRows(1);
			List<Payment> payments = rentier.getPayments();
			for (int i = 0; i < payments.size(); i++) {
				c1 = new PdfPCell(new Phrase("" + i));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(c1);
				c1 = new PdfPCell(new Phrase(Formats.formatDate(payments.get(i)
						.getPaymentDate())));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(c1);
				c1 = new PdfPCell(new Phrase(Formats.formatPounds(payments.get(
						i).getAmount())));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				table.addCell(c1);
//				if (payments.get(i).isPaied()) {
//					c1 = new PdfPCell(new Phrase("X"));
//				} else {
//					c1 = new PdfPCell(new Phrase("-"));
//				}
//				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
//				table.addCell(c1);

			}
			
			document.add(table);
			par = new Paragraph(" ");
			document.add(par);
			par = new Paragraph("Should you have any enquiries, please do not hesitate to contact me on mobile  number 07825709796",
					new Font(FontFamily.HELVETICA, 11, Font.ITALIC, blackColor) );
			par.setAlignment(Element.ALIGN_CENTER);
			document.add(par);
			
			
			par = new Paragraph(" ");
			document.add(par);

			par = new Paragraph("Kind Regards \n K S Milla",
					new Font(FontFamily.HELVETICA, 11, Font.BOLDITALIC, blackColor) );
			par.setAlignment(Element.ALIGN_LEFT);
			
			document.add(par);

			document.close();
			writer.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void print() {
		try {
			FileOutputStream fos = new FileOutputStream(new File(System.getProperty("java.io.tmpdir")+rentier.getName()+".pdf"));
			fos.write(stream.toByteArray());
			fos.close();
			
			FileInputStream is = new FileInputStream(new File(System.getProperty("java.io.tmpdir")+rentier.getName()+".pdf"));
			DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
			Doc myDoc = new SimpleDoc(is, psInFormat, null);
			PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
			aset.add(new Copies(1));
//		
//			JobName jn = new JobName(rentier.getName()+".pdf",Locale.getDefault());
//			aset.add(jn);
			PrintService pr = PrintServiceLookup.lookupDefaultPrintService();
			DocPrintJob job = pr.createPrintJob();
			
			job.print(myDoc, aset);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void exportTo(String path){
		try {
			FileOutputStream fos = new FileOutputStream(new File(path+rentier.getName()+".pdf"));
			fos.write(stream.toByteArray());
			fos.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
